#ifndef PLAYERITEM_H
#define PLAYERITEM_H

#include <QObject>
#include <QImage>
#include <QQuickPaintedItem>
#include "ffplay/Qffplayer.h"
#include <QMutex>

QImage set_cv_mosic(QImage qimage, int x, int y, int width, int height);
QImage set_amusingMirror(QImage img);
QImage add_mask(QImage img, QString mask, int x, int y);
QImage set_transform(QImage img, int x, int y);
QImage carNo(QImage src);

template <typename T>
class RingBuffer
{
public:
    RingBuffer()
    {
        return ;
    }
    ~RingBuffer()
    {
        return ;
    }
#define MAXPOS 63
    T elements[MAXPOS+1];
    int r_pos = 0;
    int w_pos = 0;

    int push(T data)
    {
        int is_full = 0;
        if(w_pos == r_pos-1 || (r_pos == 0 && w_pos == MAXPOS)){  // TODO 缓存满了如何处理，扩容 or 报错？
            qDebug() << "warning: ringbuffer is full" << endl;
            is_full = 1;
        }

        if(w_pos > MAXPOS){
            w_pos = 0;
        }

        elements[w_pos++] = data;

        return is_full;
    }
    int pop(T& data){

        if(r_pos == w_pos) // no data
            return -1;

        if(r_pos > MAXPOS){
            r_pos = 0;
        }
        data = elements[r_pos++];
        return 0;
    }

    int getSize()
    {
        if(w_pos-r_pos < 0)
            return MAXPOS+1+w_pos-r_pos;
        else
            return w_pos-r_pos;
    }
};

class PlayerItem : public QQuickPaintedItem
{
    Q_OBJECT
public:

    PlayerItem();
    ~PlayerItem();

    virtual void paint(QPainter *painter);
    Qffplayer* player;

    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mousePressEvent(QMouseEvent *e);
signals:
    void setTime2qml(int flag, QString pts, int sec);
    void stopPlay2qml(QString msg);
    void isRealTime2qml(bool isRealtime);
public slots:
    void startPlay(QString url);
    void onPlayerCb(int type, QString data, int w, int h);
    void onDisplay(QImage img, long pts);
    void stopPlay();

    QVariantMap  getMediaInfo();
    bool setFaceDetect();

    int togglePause(){
        if(isStop)
            return -1;
        if(player->setPause() != 0)
            return -1;
        isPause = !isPause;
        return 0;
    }

    int displayNextFrame()
    {
        isFrameDisplay = true;
        return player->step_to_next_frame();
    }

    void addMask(QString msg) // 加文字水印
    {
        displayImg = add_mask(displayImg,msg, assistantRec.x(), assistantRec.y());
        if(maskStr != "")
            maskStr = "";
        else
            maskStr = msg;

        if(isPause || isFrameDisplay)
        {
            if(maskStr == "")
                displayImg = orgImg;
            else
                displayImg = add_mask(displayImg, maskStr, assistantRec.x(), assistantRec.y());
            update();
        }
    }

    bool addMosic() // 加马赛克
    {
        isMosic = !isMosic;
        if(isPause || isFrameDisplay){ // 暂停或者逐帧播放

            if(isMosic)
                displayImg = set_cv_mosic(displayImg, assistantRec.x(), assistantRec.y(), assistantRec.width(), assistantRec.height());
            else
                displayImg = orgImg;
            update();
        }
        return isMosic;
    }

    bool doCarNO() // 加马赛克
    {
        isCarNO = !isCarNO;
        if(isPause || isFrameDisplay){ // 暂停或者逐帧播放

            if(isCarNO)
                displayImg = carNo(displayImg);
            else
                displayImg = orgImg;
            update();
        }
        return isCarNO;
    }

    bool getIsStop()
    {
        return isStop;
    }

    bool getIsPause()
    {
        return isPause;
    }

    bool getPlaying()
    {
        return isPlaying;
    }

    int setScale()
    {
        if((mousePressPos.x() < 0 || mouseCurrentPos.x() < 0) && isScale == false) // 还没有画框
            return -1;
        isScale = !isScale;

        if(isPause || isFrameDisplay){  // 暂停或者逐帧播放

            if(isScale){
                displayImg = orgImg.copy(assistantRec.x(), assistantRec.y(), assistantRec.width(),assistantRec.height());
                displayImg = displayImg.scaled(orgImg.width(), orgImg.height());
            }else{
                displayImg = orgImg;
            }
            update();
        }

        return isScale?1:0;
    }

    bool setTransform()  // 这个功能毫无意义
    {
        isTransform = !isTransform;
        return isTransform;
    }

    int setSeek(int sec)
    {
        return player->setPlaySeek(sec);
    }

    int setSpeed(float s)
    {
        isFrameDisplay = false;
        this->speed = s;
        return player->set_play_speed(s);
    }

    bool setRecord()
    {

        if(player)
        {
            if(isRecording ){
                isRecording = !isRecording;
                player->need_open_output = false;
            }else{

                player->need_open_output = true;
                isRecording = !isRecording;
            }
        }
    }

    bool getRecord()
    {
        return isRecording;
    }

private:
    QMutex mu;
    QImage orgImg;           // 接收到的原始图片
    QImage displayImg;       // 显示的图片
    QRect  assistantRec;     // 鼠标在播放窗口画的辅助框

    bool isStop = false;
    bool isPause = false;
    bool isPlaying = false;
    bool isScale = false;    // 局部放大
    bool isRecording = false;     // 录像
    bool isMosic = false;         // 马赛克
    bool isFrameDisplay = false;  // 逐帧播放
    float speed = 1;
    bool isTransform = false; // 图片变形
    QString maskStr = "";     // 水印文字
    bool isCarNO = false;

    QPoint mousePressPos; // 鼠标在窗口上最后一次点击的位置
    QPoint mouseCurrentPos; // 鼠标在窗口上最后一次点击的位置
    int winWidth;  // 当前窗口宽度
    int winHeight; // 当前窗口高度

    int showWidth;  // 显示宽度
    int showHeight; // 显示高度
};
#endif // PLAYERITEM_H
